package com.songcha.sakura


import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.res.Configuration
import android.graphics.Color
import android.os.Debug
import android.os.Process
import com.songcha.library_common.helper.SharedPreferenceHelper
import com.songcha.library_common.util.ScreenUtil
import com.songcha.sakura.download.DownloadServiceController
import com.songcha.sakura.download.IServiceConnectionListener
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshFooter
import com.scwang.smart.refresh.layout.api.RefreshHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.DefaultRefreshFooterCreator
import com.scwang.smart.refresh.layout.listener.DefaultRefreshHeaderCreator
import com.songcha.library_business.LibraryBusinessApplication
import com.songcha.library_common.LibraryCommon
import com.songcha.library_common.util.ProcessUtil
import com.songcha.sakura.config.BaseConfig
import com.songcha.library_database_sakura.LibraryDatabaseSakuraApplication
import com.songcha.sakura.ui.view.refresh.CustomRefreshHeader
import me.jessyan.autosize.AutoSizeConfig
import me.jessyan.autosize.onAdaptListener


class MyApplication :Application(){

    companion object{
        private lateinit var mContext:Application
        //private lateinit var mSession:DaoSession
        @JvmStatic
        fun getContext():Context{
            return mContext
        }

       /* fun getDaoSession():DaoSession{
            return mSession
        }*/
        //防止内存泄漏
        init {
           SmartRefreshLayout.setDefaultRefreshHeaderCreator(object: DefaultRefreshHeaderCreator  {
               override fun createRefreshHeader(context:Context, layout:RefreshLayout):RefreshHeader {
                   //layout.setPrimaryColorsId(R.color.primary, R.color.white)//全局设置主题颜色
                   //return ClassicsHeader(context)
                   return CustomRefreshHeader(context)
               }
           })
           //设置全局的Footer构建器
           SmartRefreshLayout.setDefaultRefreshFooterCreator(object:DefaultRefreshFooterCreator  {
               override fun createRefreshFooter(context:Context, layout:RefreshLayout): RefreshFooter{
                   //指定为经典Footer，默认是 BallPulseFooter
                   //val footer=ClassicsFooter(context).setDrawableSize(20f)
                   //footer.setBackgroundColor(ColorUtil.getResourceColor(context,R.color.background))
                   //return footer
                   return ClassicsFooter(context).setDrawableSize(20f).apply {
                       setBackgroundColor(Color.TRANSPARENT)
                   }
               }

           })
        }

        @JvmStatic
        private fun init(){

           /* ARouter.openLog()
            ARouter.openDebug()
            ARouter.init(mContext)

            ModuleReaderApplication.init(mContext)
            LibraryCommon.init(mContext,BuildConfig.DEBUG)*/
            LibraryBusinessApplication.init(mContext)
            LibraryDatabaseSakuraApplication.init(mContext)



          /*  val devOpenHelper =  DaoMaster.DevOpenHelper(mContext, "greendao.db")
            val db = devOpenHelper.writableDatabase
            val daoMaster =  DaoMaster(db)
            mSession = daoMaster.newSession()*/


            var baseUrl= SharedPreferenceHelper.getString("setting","base_url")
            var fontPath= SharedPreferenceHelper.getString("setting","font_path")
            if(baseUrl.isNotEmpty()){
                BaseConfig.BASE_URL=baseUrl
            }
            if(fontPath.isNotEmpty()){
                BaseConfig.FONT_PATH=fontPath
            }

            //横竖屏适配，但旋转屏幕时无法正确获取
            AutoSizeConfig.getInstance().setOnAdaptListener(object : onAdaptListener{
                override fun onAdaptBefore(target: Any?, activity: Activity?) {
                    AutoSizeConfig.getInstance().screenWidth= ScreenUtil.getScreenWidth(activity!!)
                    AutoSizeConfig.getInstance().screenHeight=ScreenUtil.getScreenHeight(activity!!)
                    if(activity.resources.configuration.orientation==Configuration.ORIENTATION_LANDSCAPE){
                        AutoSizeConfig.getInstance().isBaseOnWidth=false
                        AutoSizeConfig.getInstance().designWidthInDp=640
                        AutoSizeConfig.getInstance().designHeightInDp=360
                        //AutoSize.autoConvertDensityBaseOnHeight(activity!!,360f)
                    }else{
                        AutoSizeConfig.getInstance().isBaseOnWidth=true
                        AutoSizeConfig.getInstance().designWidthInDp=360
                        AutoSizeConfig.getInstance().designHeightInDp=640
                        //AutoSize.autoConvertDensityBaseOnWidth(activity!!,360f)
                    }
                }

                override fun onAdaptAfter(target: Any?, activity: Activity?) {

                }

            })

        }

    }


    override fun onCreate() {
        super.onCreate()

        mContext =this

        if(Debug.isDebuggerConnected())
            Debug.waitForDebugger()

        val progressName= ProcessUtil.getAppNameByPID(this,Process.myPid())
        //LogUtil.log("application oncreate",progressName)

        if(progressName==applicationInfo.packageName){
            init()

            Thread{
                DownloadServiceController.bindService(object: IServiceConnectionListener {
                    override fun onConnected() {
                    }

                    override fun onConnectInitialize() {
                        DownloadServiceController.getLocalTaskList()
                    }

                })
            }.start()

        }else if(progressName.endsWith(":remote_download")){
            LibraryCommon.init(
                mContext
            )
        }



    }









}